package com.artisan.web.action;

import com.alibaba.fastjson.JSON;
import com.artisan.common.enums.OrderStateEnum2;
import com.artisan.common.pojo.PageBean;
import com.artisan.common.utils.TimeFormatUtil;
import com.artisan.commonweb.result.ActionResult;
import com.artisan.medium.admin.service.OrderOverViewService;
import com.artisan.model.AddressInfos;
import com.artisan.model.Business;
import com.artisan.model.Worker;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * Created by szw on 2017/6/29.
 */
@Controller
@RequestMapping("/admin/order/")
public class OrderOverViewController {

    private static final Logger log = LogManager.getLogger(OrderOverViewController.class);

    @Autowired
    private OrderOverViewService orderOverViewService;

    /**
     * 订单统计
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("OrderOverView")
    public ModelAndView orderOverView(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        result = orderOverViewService.orderStatistics();
        result.put("up", OrderStateEnum2.WAITING_PAY.getValue());
        result.put("ud", OrderStateEnum2.WAITING_ORDER.getValue());
        result.put("uc", OrderStateEnum2.FINISHED.getValue());
        result.put("ur", OrderStateEnum2.IN_AUDIT.getValue());
        //result.put("date", new Date());
        return new ModelAndView("order/order_overview", result);
    }

    /**
     * 查询订单列表
     * @param request
     * @param response
     * @param orderNo
     * @param phone
     * @param busaId
     * @param busbId
     * @param buscId
     * @param startTime
     * @param endTime
     * @param state
     * @param provId
     * @param cityId
     * @param areaId
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "queryOrders", method = RequestMethod.GET)
    public Object queryOrders(HttpServletRequest request, HttpServletResponse response, String orderNo, String phone,
                                    Integer busaId, Integer busbId, Integer buscId, String startTime, String endTime, Integer state,
                                    String provId, String cityId, String areaId, @RequestParam(defaultValue = "0") Integer offset,
                                    @RequestParam(defaultValue = "10") Integer limit) {

        //Map<String, Object> result = new HashMap<>();
        PageBean pageBean = orderOverViewService.queryOrders(orderNo, phone, busaId, busbId, buscId, startTime, endTime, state,
                provId, cityId, areaId, offset/limit + 1, limit);
        return pageBean;
    }

    /**
     * 查询订单详情
     * @param request
     * @param response
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "qOrderDetail", method = RequestMethod.GET)
    public ModelAndView qOrderDetail(HttpServletRequest request, HttpServletResponse response, String orderNo) {

        Map<String, Object> result = orderOverViewService.qOrderDetail(orderNo);
        return new ModelAndView("order/order_detail", result);
    }

    /**
     * 动态查询业务分类
     * @param request
     * @param response
     * @param pid
     * @param tag
     * @return
     */
    @RequestMapping("qBusiness")
    public String qBusinessInfos(HttpServletRequest request, HttpServletResponse response, Integer pid, String tag) {
        List<Business> busList = orderOverViewService.qBusiness(pid, tag);
        return JSON.toJSONString(busList);
    }

    /**
     * 动态查询市或者区
     * @param request
     * @param response
     * @param pid
     * @return
     */
    @RequestMapping("qAddress")
    public String qAddress(HttpServletRequest request, HttpServletResponse response, String pid) {
        List<AddressInfos> addrList = orderOverViewService.qAddress(pid);
        return JSON.toJSONString(addrList);
    }

    /**
     * 查询符合条件的待派单工人
     * @param orderNo
     * @return
     */
    @ResponseBody
    @RequestMapping("query_workers")
    public ActionResult queryWorkers(String orderNo) {
        Map<String, Object> result = new HashMap<>();

        ActionResult actionResult = new ActionResult();
        try {
            List<Worker> workers = orderOverViewService.queryWorkers(orderNo);

            if (workers != null) {
                result.put("workers", workers);
                result.put("orderNo2", orderNo);
                actionResult.setCode(200);
                actionResult.setData(result);
            } else {
                actionResult.setCode(300);
            }

        }catch (Exception e){
            actionResult.setCode(400);
            actionResult.setHint(e.getMessage());
        }
        return actionResult;
    }

    /**
     * 派单
     * @param request
     * @param response
     * @param orderNo
     * @param workerId
     * @return
     */
    @ResponseBody
    @RequestMapping("dispatchOrder")
    public ActionResult dispatchOrder(HttpServletRequest request, HttpServletResponse response, String orderNo, Integer workerId) {

        ActionResult result = new ActionResult();
        try {
            int count = orderOverViewService.disPatchOrder(orderNo, workerId);
            if (count == 1) {
                result.setCode(200);
            } else {
                result.setCode(300);
            }
        } catch (Exception e) {
            result.setCode(400);
            result.setHint("派单失败." + e.getMessage());
        }

        return result;
    }

    @RequestMapping("order_list_page")
    public ModelAndView orderPageList(@RequestParam(defaultValue = "-1") Integer state, @RequestParam(defaultValue = "false") boolean today) {
        Map<String, Object> result = new HashMap<>();
        result.put("state", state);
        if (today) {
            result.put("date", TimeFormatUtil.dateToStrToDay(new Date()));
        } else {
            result.put("date", "");
        }
        return new ModelAndView("order/order_list", result);
    }

    @RequestMapping("order_detail_page")
    public ModelAndView orderDetailPage() {
        return new ModelAndView("order/order_detail");
    }

    @ResponseBody
    @RequestMapping("query_state")
    public ActionResult queryState() {
        ActionResult actionResult = new ActionResult();
        try {
            List<Map<String, Object>> list = new ArrayList<>();
            OrderStateEnum2[] states = OrderStateEnum2.values();
            for(OrderStateEnum2 os : states) {
                Map<String, Object> map = new HashMap<>();
                map.put("v", os.getValue());
                map.put("o", os.getaState());
                list.add(map);
            }
            actionResult.setCode(200);
            actionResult.setData(list);
        }catch (Exception e){
            actionResult.setCode(400);
            actionResult.setHint(e.getMessage());
        }
        return actionResult;
    }

    @ResponseBody
    @RequestMapping("confirmOrderFinished")
    public ActionResult confirmOrderFinished(String orderNo) {
        ActionResult result = new ActionResult();
        try {
            int count = orderOverViewService.confirmOrderFinished(orderNo);
            if (count == 1) {
                result.setCode(200);
            } else {
                result.setCode(300);
            }
        } catch (Exception e) {
            result.setCode(400);
            result.setHint("订单确认失败." + e.getMessage());
        }
        return result;
    }
}
